package jlxy.ise.eroad.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Component;

/**
 * JdbcTemplate的sql操作器
 *
 * @author yecq
 */
@Component
@Scope("prototype")
public class SqlOperator {

    private static boolean log = false;

    public static void setSqlShow(boolean b) {
        log = b;
    }

    @Autowired
    private JdbcTemplate jdbc;

    public List<Map<String, Object>> query(String stmt, Object... args) {
        if (log) {
            System.out.println(stmt);
        }
        return this.jdbc.query(stmt, args, new RowMapper<Map<String, Object>>() {

            @Override
            public Map<String, Object> mapRow(ResultSet rs, int i) throws SQLException {
                Map<String, Object> map = new HashMap();
                ResultSetMetaData meta = rs.getMetaData();
                int cols = meta.getColumnCount();
                for (int k = 1; k <= cols; k++) {
                    map.put(meta.getColumnLabel(k), rs.getObject(k));
                }
                return map;
            }
        });
    }

    public String[] insert(String stmt, final Object... args) {
        if (log) {
            System.out.println(stmt);
        }
        final String stmt1 = stmt;
        KeyHolder kh = new GeneratedKeyHolder();
        this.jdbc.update(new PreparedStatementCreator() {

            @Override
            public PreparedStatement createPreparedStatement(Connection conn) throws SQLException {
                PreparedStatement ps = conn.prepareStatement(stmt1, Statement.RETURN_GENERATED_KEYS);
                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i + 1, args[i]);
                }
                return ps;
            }
        }, kh);
        List<Map<String, Object>> ret = kh.getKeyList();
        String[] ids = new String[ret.size()];
        int i = 0;
        Iterator<Map<String, Object>> ite = ret.iterator();
        while (ite.hasNext()) {
            Map<String, Object> map = ite.next();
            ids[i++] = map.get("GENERATED_KEY") + "";
        }
        return ids;
    }

    public int update(String stmt, Object... args) {
        if (log) {
            System.out.println(stmt);
        }
        return this.jdbc.update(stmt, args);
    }

    public int delete(String stmt, Object... args) {
        if (log) {
            System.out.println(stmt);
        }
        return this.jdbc.update(stmt, args);
    }
}
